using System;
using BitMango.Diagnostics;

namespace BitMango
{
	public class MSRandom
	{
		public MSRandom()
		{
			DateTime d = DateTime.Now.ToLocalTime();
			DateTime dateTime = new DateTime(1970, 1, 1);
			DateTime d2 = dateTime.ToLocalTime();
			this.seed = (long)(d - d2).TotalSeconds;
		}

		public MSRandom(long _seed)
		{
			this.seed = _seed;
		}

		public int Next()
		{
			return (int)this.RandomTable() % int.MaxValue;
		}

		public int Next(int max)
		{
			return (int)this.RandomTable() % max;
		}

		public int Next(int min, int max)
		{
			Debugger.Assert(min <= max, "BitMango.MSRandom - Min can not be larget than the Max.");
			return (int)this.RandomTable() % (max - min) + min;
		}

		private long RandomTable()
		{
			this.seed = this.seed * 214013L + 2531011L;
			return this.seed >> 16 & 32767L;
		}

		private long seed;
	}
}
